package org.cttic.visualization.visualization.util;

import org.cttic.visualization.visualization.common.RouteIdentificationData;
import org.cttic.visualization.visualization.common.T_mysqlData;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-13 13:38
 * 查询mysql基础信息表
 */
@Component
public class jdbcUtil {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public void mysqlUtile(int pointType,String gantryId,RouteIdentificationData.Identification identification){
        //门架 省界门架
        if (pointType == 1||pointType==2) {
            String sql = "SELECT gantryId,provinceId,tollIntervals,roadSectionId,pileNumber FROM `T_GANTRY_MOUNTING` " +
                    "where  gantryId= '" + gantryId + "'";
            List<T_mysqlData> mysqlData = jdbcTemplate.query(sql, new BeanPropertyRowMapper(T_mysqlData.class));
            if(mysqlData!=null&&mysqlData.size()>0){

                T_mysqlData t_mysqlData = mysqlData.get(0);
                identification.setSectionCode(t_mysqlData.getRoadSectionId());
                identification.setPileNum(t_mysqlData.getPileNumber());
                identification.setDirection(gantryId.substring(14, 15));
                String tollIntervals = t_mysqlData.getTollIntervals();

                identification.setTollIntervals(tollIntervals);
                identification.setProvinceId(t_mysqlData.getProvinceId());
                identification.setSectionId(t_mysqlData.getRoadSectionId());
                identification.setProvince(provinceData.province.get(t_mysqlData.getProvinceId()));

                if(!tollIntervals.contains("|")){
                    String sql2 ="SELECT unitId,provinceId,unitLength FROM `T_CHARGING_UNIT` where  unitId= '"+tollIntervals+"'";
                    List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql2);
                    if(maps!=null&&maps.size()>0){
                        Map<String, Object> map = maps.get(0);
                        Object unitLength = map.get("unitLength");
                        double value = Double.parseDouble(String.valueOf(unitLength)) / 1000;//单位m 需转km

                        identification.setBillMile(value+"");
                    }

                }else {
                    Double BillAm=0.00D;
                    String[] split = tollIntervals.split("\\|");
                    for (String s : split) {
                        String sql3 ="SELECT unitId,provinceId,unitLength FROM `T_CHARGING_UNIT` where  unitId= '"+s+"'";
                        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql3);
                        if(maps!=null&&maps.size()>0){
                            Map<String, Object> map = maps.get(0);
                            Object unitLength = map.get("unitLength");
                            BillAm+=Double.parseDouble(String.valueOf(unitLength))/1000;//单位m 需转km
                        }
                    }
                    identification.setBillMile(BillAm+"");
                }
            }
        } else if (pointType == 3) {
            String sql = "SELECT stationId,provinceId,stakeNum FROM `T_STATION` " +
                    "where  stationId= '" +
                    gantryId + "'";

            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
            if(maps!=null&&maps.size()>0){

                Map<String, Object> stringObjectMap = maps.get(0);
//            sectionId = stringObjectMap.get("stationId").toString();
//                        provinceId = stringObjectMap.get("provinceId").toString();
                String pileNumber = stringObjectMap.get("stakeNum") == null ? "": stringObjectMap.get("stakeNum").toString();

                identification.setPileNum(pileNumber);
//                        identification.setDirection(gantryId.substring(14, 15));
            }


        }

    }
}
